package com.yugao.fintech.libra.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.libra.common.ram.ActionType;
import com.yugao.fintech.libra.common.ram.RamResource;
import com.yugao.fintech.libra.system.model.rqrs.article.ArticleBasePageReq;
import com.yugao.fintech.libra.system.model.rqrs.article.ArticleResp;
import com.yugao.fintech.libra.system.model.rqrs.article.ArticleSaveReq;
import com.yugao.fintech.libra.system.model.rqrs.article.ArticleUpdateReq;
import com.yugao.fintech.libra.system.service.ArticleService;
import com.yugao.fintech.meheco.core.domain.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Tag(name = "文章")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Operation(description = "分页文章")
    @GetMapping("/pageArticle")
    @RamResource(action = "sys:article:query", name = "查询文章", type = ActionType.READ)
    public ResultVO<IPage<ArticleResp>> pageArticle(@Validated ArticleBasePageReq req) {
        return ResultVO.success(articleService.pageArticle(req));
    }

    @Operation(description = "通过id查找文章")
    @GetMapping("/getArticle")
    @RamResource(action = "sys:article:query", name = "查询文章", type = ActionType.READ)
    public ResultVO<ArticleResp> getArticle(@RequestParam(value = "id") Long id) {
        return ResultVO.success(articleService.getArticle(id));
    }
  
    @Operation(description = "保存文章")
    @PostMapping("/saveArticle")
    @XssCleanIgnore({"content", "title"})
    @RamResource(action = "sys:article:save", name = "保存文章", type = ActionType.WRITE)
    public ResultVO<?> saveArticle(@Validated @RequestBody ArticleSaveReq req) {
        return ResultVO.success(articleService.saveArticle(req));
    }
  
    @Operation(description = "更新文章")
    @PostMapping("/updateArticle")
    @XssCleanIgnore({"content", "title"})
    @RamResource(action = "sys:article:update", name = "更新文章", type = ActionType.WRITE)
    public ResultVO<?> updateArticle(@Validated @RequestBody ArticleUpdateReq req) {
        return ResultVO.success(articleService.updateArticle(req));
    }
  
    @Operation(description = "批量删除文章")
    @DeleteMapping("/batchDeleteArticle")
    @RamResource(action = "sys:article:delete", name = "删除文章", type = ActionType.WRITE)
    public ResultVO<?> batchDeleteArticle(@RequestParam(value = "ids") List<Long> ids) {
        return ResultVO.success(articleService.batchDeleteArticle(ids));
    }
  
    @Operation(description = "删除文章")
    @DeleteMapping("/deleteArticle")
    @RamResource(action = "sys:article:delete", name = "删除文章", type = ActionType.WRITE)
    public ResultVO<?> deleteArticle(@RequestParam(value = "id") Long id) {
        return ResultVO.success(articleService.deleteArticle(id));
    }
}
